<template>
  <!-- 出入原始记录查询列表 -->
  <div class="mod-role" style="width:100%">
    <el-form :inline="true" :model="dataForm">
      <el-form-item label="人员：">
        <el-input v-model="dataForm.roleName" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="部门：" prop="parentName">
        <el-popover ref="menuListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="menuId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:menuListPopover
          :readonly="true"
          placeholder="点击选择上级菜单"
          class="menu-list__input"
        ></el-input>
      </el-form-item>
      <el-form-item label="设备">
        <el-select v-model="eqId" placeholder="请选择">
          <el-option
            v-for="(item,index) in antuList"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <br />
      <el-form-item label="时间：">
        <!-- <el-date-picker
          v-model="value1"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>-->
        <el-date-picker
          v-model="value1"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="选择日期时间"
        ></el-date-picker>至
        <el-date-picker
          v-model="value2"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="选择日期时间"
        ></el-date-picker>
      </el-form-item>

      <el-button type="primary" @click="getTableList">搜索</el-button>
      <!-- <el-button style="margin-right:10px;">取消</el-button> -->
      <el-form-item>
        <el-button
          style="margin-left:10px"
          v-if="isAuth('sys:office:export')"
          type="primary"
          @click="exportEx()"
        >导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      stripe
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
      <el-table-column prop="departName" header-align="center" align="center" label="部门"></el-table-column>
      <el-table-column prop="roleName" header-align="center" align="center" width="100" label="角色名"></el-table-column>
      <el-table-column prop="name" align="center" label="姓名" width="90px"></el-table-column>
      <el-table-column prop="cardStr" header-align="center" width="120" align="center" label="卡号"></el-table-column>
      <el-table-column
        prop="licenseNum"
        header-align="center"
        width="120"
        align="center"
        label="车牌号"
      ></el-table-column>

      <el-table-column
        prop="time"
        header-align="center"
        show-overflow-tooltip
        align="center"
        label="识别时间"
      ></el-table-column>
      <el-table-column
        prop="deviceName"
        header-align="center"
        show-overflow-tooltip
        align="center"
        label="进入设备"
      ></el-table-column>
      <el-table-column
        prop="deviceNo"
        show-overflow-tooltip
        header-align="center"
        align="center"
        label="设备ID"
      ></el-table-column>
      <el-table-column
        prop="deviceIp"
        show-overflow-tooltip
        header-align="center"
        align="center"
        label="设备IP"
      ></el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "../baseData/role-add-or-update";

import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      antuList: [],
      dataForm: {
        roleName: "",
        parentName: "",
        departId: ""
      },
      menuListTreeProps: {
        label: "lable",
        children: "children"
      },
      value1: new Date(new Date(new Date().toLocaleDateString()).getTime()),
      value2: new Date(),
      dataList: [],
      menuList: [],
      pageIndex: 1,
      eqId: "",
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
    this.$http({
      url: this.$http.adornUrl("/sys/car/info"),
      method: "POST"
    }).then(({ data }) => {
      this.antuList = data.gates;
    });
  },
  methods: {
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      if (data.type == "0" && data.parentId) {
        this.dataForm.departId = data.value;
        this.dataForm.parentName = data.lable;
      } else {
        this.dataForm.departId = "";
        this.dataForm.parentName = "";
      }
    },
    exportEx() {
      let data = {
        beginDate: this.dateToString(this.value1),
        endDate: this.dateToString(this.value2),

        departId: this.dataForm.departId,
        name: this.dataForm.roleName
      };
      this.$confirm(`确定导出选中数据操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/sys/employee/export_list"),
          method: "post",
          responseType: "blob",
          data: this.$http.adornData({
            data: JSON.stringify(data)
          })
        }).then(res => {
          console.log(res);
          var blob = new Blob([res.data], {
            type: "application/vnd.ms-excel"
          });
          var downloadElement = document.createElement("a");
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "导出文件.xls"; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        });
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sys/org/list"),
        method: "post",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.menuList = data.list;
      });
      this.getTableList();
    },
    getTableList() {
      console.log(this.value1);
      let data = {
        beginDate: this.dateToString(this.value1),
        endDate: this.dateToString(this.value2),

        departId: this.dataForm.departId,
        name: this.dataForm.roleName,
        eqId: this.eqId,
        page: this.pageIndex,

        size: this.pageSize
      };
      this.$http({
        url: this.$http.adornUrl("/sys/employee/distinguishlist"),
        method: "post",
        data: this.$http.adornData({
          data: JSON.stringify(data)
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.roleId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sys/role/delete"),
            method: "post",
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    }
  }
};
</script>
